/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.desktop.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springblade.desktop.entity.DeptSync;
import org.springblade.desktop.mapper.DeptSyncMapper;
import org.springblade.desktop.service.IDeptSyncService;
import org.springblade.desktop.utils.MianUtils;
import org.springblade.desktop.vo.DeptSyncVO;
import org.springblade.desktop.vo.MyResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.*;

/**
 *  服务实现类
 *
 * @author BladeX
 * @since 2021-09-18
 */
@Service
public class DeptSyncServiceImpl extends ServiceImpl<DeptSyncMapper, DeptSync> implements IDeptSyncService {

	@Autowired
	private DeptSyncMapper deptSyncMapper;

	@Override
	public IPage<DeptSyncVO> selectDeptSyncPage(IPage<DeptSyncVO> page, DeptSyncVO deptSync) {
		return page.setRecords(baseMapper.selectDeptSyncPage(page, deptSync));
	}

	//获取部门列表
	@Override
	@Transactional
	public int instrtDeptAll(String accessToken) {
		try {
			MyResult myResult = MianUtils.mainPoUtils("https://oapi.dingtalk.com/topapi/v2/department/listsub", accessToken, new HashMap<String, Object>());
			if("ok".equals(myResult.getErrmsg())){
				String result = myResult.getResult();
				List<DeptSync> lists=new ArrayList<>();
				lists.add(new DeptSync(1L,0L,"北京群鲜荟萃科技有限公司",1L));
				List<DeptSync> deptSyncList = JSONArray.parseArray(result, DeptSync.class);
				recursion(deptSyncList,accessToken,lists,new HashSet<>());
				deptSyncList.addAll(lists);
				boolean b = this.saveBatch(deptSyncList);
				return 1;
			}
			return -1;
		}catch (Exception e){
			e.printStackTrace();
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			return -2;
		}
	}

	//递归
	private void recursion(List<DeptSync> deptSyncList, String accessToken, List<DeptSync> lists, Set<Long> sets){
		if (deptSyncList!=null && !deptSyncList.isEmpty()){
			for(DeptSync list:deptSyncList){
				if (!sets.contains(list.getDeptId())){
					MyResult result = MianUtils.mainPoUtils("https://oapi.dingtalk.com/topapi/v2/department/listsub", accessToken, new HashMap<String, Object>(){{put("DeptId",list.getDeptId());put("language","zh_CN");}});
					sets.add(list.getDeptId());
					if(result.getErrcode()!=40009){
						if("ok".equals(result.getErrmsg())){
							String result1 = result.getResult();
							if (result1!=null && !"".equals(result1) && !"[]".equals(result1)){
								List<DeptSync> syncList = JSONArray.parseArray(result1, DeptSync.class);
								lists.addAll(syncList);
								//deptSyncList.addAll(syncList);
								recursion(syncList,accessToken,lists,sets);
							}
						}
					}
				}
			}
		}
	}
}
